package com.jason.platform.supervision.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.task.HttpConfig;
import com.jason.platform.supervision.utils.SharedPreferencesUtils;
import com.jason.platform.supervision.widght.TitleBar;

/**
 * @ClassName: ConfigActivity
 * @Description: 系统配置
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-25 09:51:05
 */
public class ConfigActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = ConfigActivity.class.getSimpleName();

    private TitleBar mTitleBar;

    private EditText mIpEditText,mPortEditText,mAppNameEditText;
    private Button mClearButton,mSaveButton;

    @Override
    void handleMessage(BaseActivity activity, Message msg) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_config);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.bindActivity(this);
        mIpEditText = (EditText) findViewById(R.id.config_system_ip);
        mPortEditText = (EditText) findViewById(R.id.config_system_port);
        mAppNameEditText = (EditText) findViewById(R.id.config_system_app_name);
        mClearButton = (Button) findViewById(R.id.config_clear_btn);
        mSaveButton = (Button) findViewById(R.id.config_save_btn);
        mClearButton.setOnClickListener(this);
        mSaveButton.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        mIpEditText.setText(SharedPreferencesUtils.getHttpHost(this));
        mPortEditText.setText(SharedPreferencesUtils.getHttpPort(this));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.config_clear_btn){
            mIpEditText.setText("");
            mPortEditText.setText("");
            mAppNameEditText.setText("");
            Toast.makeText(this,"清空完成",Toast.LENGTH_SHORT).show();
        }else if (v.getId() == R.id.config_save_btn){
            String ip = mIpEditText.getText().toString();
            String port =  mPortEditText.getText().toString();
            if (TextUtils.isEmpty(ip) || TextUtils.isEmpty(port)){
                Toast.makeText(this,"服务器IP或者端口不能为空",Toast.LENGTH_SHORT).show();
            }else {
                //初始化当前ip,端口
                SharedPreferencesUtils.setHttpHost(this,ip);
                SharedPreferencesUtils.setHttpPort(this,port);
                HttpConfig.setHost(SharedPreferencesUtils.getHttpHost(this));
                HttpConfig.setPort(SharedPreferencesUtils.getHttpPort(this));
                Toast.makeText(this,"保存完成",Toast.LENGTH_SHORT).show();
            }

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mIpEditText.getWindowToken(), 0);
    }
}
